package com.aloda.handler;

import com.aloda.constant.ExceptionConstant;
import com.aloda.exception.AuthException;
import com.aloda.exception.BaseException;
import com.aloda.result.Result;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理器，处理项目中抛出的业务异常
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 捕获业务异常
     * @param ex
     * @return
     */
    @ExceptionHandler
    public Result exceptionHandler(BaseException ex){
        return Result.error(ex.getErrorCode(),ex.getMessage());
    }

    /**
     * 捕获权限异常
     * @param ex
     * @return
     */
    @ExceptionHandler
    public Result exceptionHandler(AuthException ex) {
        return Result.error(ExceptionConstant.AUTH_ERROR_CODE,ex.getMessage());
    }

    /**
     * 捕获未定义的异常
     * @param ex
     * @return
     */
    @ExceptionHandler
    public Result exceptionHandler(RuntimeException ex) {
        // TODO: 打印异常日志
        ex.printStackTrace();
        return Result.error(ExceptionConstant.SYSTEM_EXCEPTION);
    }
}
